<%@ include file="/WEB-INF/views/taglib.jsp" %>
<h3>Serial Number Updation Form</h3>	
	<form:form id="form" method="post" modelAttribute="assetEquipment"  cssClass="simpleForm">
	<input name="success" id="success" type="hidden" value="${success}"/>
		<form:hidden path="assetEquipmentPK" />
		<div class="clear" style="height:1px">&nbsp;</div>
		<jsp:include page="/WEB-INF/views/message.jsp"></jsp:include>
		<c:choose>
				<c:when test="${success!='Y'}">
			  	<fieldset>
			  		<legend>Update Serial No</legend>
			  		<p>
			  		<aims:column cssErrorClass="error" path="assetEquipmentPK" >
			  		<form:label path="assetEquipmentPK">
			  			Old Serial No 
			 		</form:label>
			  		<form:input path="assetEquipmentPK.serialNo" id = "sNo" size="30"/> <form:errors path="assetEquipmentPK"  cssClass="errorSimple" /> 
					</aims:column>
					
					
					<aims:column cssErrorClass="error" path="newSerialNo" >
			  		<form:label path="newSerialNo">
			  			New Serial No 
			 		</form:label>
			  		<form:input path="newSerialNo" size="30"/> <form:errors path="newSerialNo"  cssClass="errorSimple" /> 
					</aims:column>
					
					</p>
					</fieldset>
				</c:when>
					
				<c:otherwise>
					<fieldset>
					<p>
					<label>
			  			Old Serial No 
			 		</label>
			 		<c:out value="${oldSerialNo}"/>		  		
			 		</p>
			 		<p>
			 		<label>
			 		New Serial No
			 		</label>	
			 			<c:out value="${assetEquipment.assetEquipmentPK.serialNo}"/>
					</p>
					</fieldset>
					
				</c:otherwise>
			</c:choose>
			
		<div class="action">
		
				<button type="button" class="button positive" onclick="updateToFaulty()">
	  			Update to Faulty
	  			</button>
			  	
			  	<c:if test="${success!='Y'}">
	  			<button type="button" class="button positive" onclick="checkAvailability('check')">
	  			Check Availability
	  			</button>
	  			
	  			<button type="button" id="updateButton" class="button positive" onclick="checkAvailability('update')">
	  			Update
	  			</button>
	  			</c:if>
	  			
	  			<a href="<c:url value="/assetModel/serialNoSearch" />" class="button">Back</a>
	  		
	 </div>	
			
      
	</form:form>
	
	<script type="text/javascript">
	$(document).ready(function() {
	<c:if test="${assetEquipment.assetEquipmentPK.serialNo!=null }">
	readonlyAttr('#sNo');
	</c:if>	
	$('#updateButton').attr('disabled', 'disabled');
	});
	

	function updateToFaulty()
	{
		var url = "<c:url value="/assetModel/updateToFaulty/${assetEquipment.assetEquipmentPK.serialNo}"/>";
		var form = $('#form');				
		redirect(form,url);		
	}
	
	function redirect(form,url){		
		form.attr('action',url );
		form.submit();
	}
	
	
	function checkAvailability(value)
	{
	
		var newSerialNo =  $('#newSerialNo').val();
		
		if(newSerialNo == ''){
			alert('Pls do enter new serial no');			
		}
		
		else {		
				$.getJSON("<c:url value="/ajax/serialno/availability"></c:url>?serialNo="+newSerialNo, function(data) {
					  
					 if(data == false){
						 		alert('Asset already exist with the same Serial No ! \n Try a different Serial No.');						 
					}
					 else{
						
						 	if(value != 'update'){
						 		alert('Entered Serial No is available ! \n Please proceed updating');
						 		$('#updateButton').removeAttr('disabled');

						 	}
						 	
						 
						 	else{
							 	var form = $('#form');
								form.submit();	
							 }
					}
					
				});
		}
		
		
	}
	
		
	</script>